﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;
using System.Data.Common;

namespace QLHPHP
{
    /// <summary>
    /// Class về các tham số
    /// </summary>
    public static class ThamSo
    {
        static ThamSo()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DataTable LayThamSo()
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "GetConstant";
            return DataAccess.ExecuteSelectCommand(comm);
        }

        /// <summary>
        /// Them tham so
        /// </summary>
        /// <param name="constantName">Ten tham so</param>
        /// <param name="value">Gia tri cua tham so</param>
        /// <returns></returns>
        public static bool ThemThamSo(string constantName, float value)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "CreateConstant";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@TenThamSo";
            param.DbType = DbType.String;
            param.Value = constantName;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@GiaTri";
            param.DbType = DbType.Double;
            param.Value = value;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Cap nhat gia tri tham so
        /// </summary>
        /// <param name="constantName">Ten tham so</param>
        /// <param name="value">Gia tri tham so</param>
        /// <returns></returns>
        public static bool CapNhatThamSo(string constantName, float value)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "UpdateConstant";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@TenThamSo";
            param.DbType = DbType.String;
            param.Value = constantName;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@GiaTri";
            param.DbType = DbType.Double;
            param.Value = value;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }
        /// <summary>
        /// Loai bo 1 tham so khoi danh sach
        /// </summary>
        /// <param name="constantName">Ten tham so</param>
        /// <returns></returns>
        public static bool XoaThamSo(string constantName)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "DeleteConstant";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@TenThamSo";
            param.DbType = DbType.String;
            param.Value = constantName;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }
    }
}
